﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace TRPOP.Common
{
    /// <summary>
    /// CMSUploadHelper
    /// </summary>
    public class CMSUploadHelper : UploadHelper
    {
        /// <summary>
        /// 单个文件上传
        /// </summary>
        /// <param name="MaxFileSize">文件大小限制</param>
        /// <param name="path">上传目录</param>
        /// <param name="AllowType">文件类型限制</param>
        /// <returns></returns>
        static public AttachmentInfo CMSFieldUpLoad(int MaxFileSize, string path,  string AllowType)
        {
            HttpFileCollection MyFileColl = HttpContext.Current.Request.Files;
            AttachmentInfo oaiInfo = new AttachmentInfo();
            bool blError = false;
            if (MyFileColl.Count > 0)
            {
               
                #region 类型判断(判断是否为系统允许的上传类型,判断文件大小)

                string strFileName = MyFileColl[0].FileName;
                //文件扩展名
                string strFileExtendName = strFileName.Substring(strFileName.LastIndexOf(".") + 1).ToLower();
                //文件真实类型
                string strFileType = MyFileColl[0].ContentType;
                //文件大小
                int intFileSize = MyFileColl[0].ContentLength;
                if (AllowType != "")
                {
                    if (strFileExtendName == "" || AllowType.IndexOf(strFileExtendName) == -1)
                    {
                        oaiInfo.ErrorDescription = "对不起，你上传的文件类型有误，允许的类型为" + AllowType;
                        blError = true;
                    }
                }
                if (MaxFileSize > 0)
                {
                    if (intFileSize == 0 || intFileSize > MaxFileSize)
                    {
                        oaiInfo.ErrorDescription = "对不起，上传文件最大为" + (MaxFileSize / 1000) + "K，请重新选择上传！";
                        blError = true;
                    }
                }
                if (blError)
                {
                    return oaiInfo;
                }
                #endregion

                return UploadOneFile(MyFileColl[0], path);
            }
            else
            {
                oaiInfo.ErrorDescription = "没有选择上传的文件";
                return oaiInfo;
            }
        }

        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="postedFile">上传控件</param>
        /// <param name="upLoadForder">上传目录，相对于根目录，前不用加"/"</param>
        /// <returns></returns>
         private static AttachmentInfo UploadOneFile(HttpPostedFile postedFile, string upLoadForder)
        {
            AttachmentInfo oaiInfo = new AttachmentInfo();
            string strRootFolder = "/";
            //文件名
            string strFileName = postedFile.FileName;
            //文件扩展名
            string strFileExtendName = strFileName.Substring(strFileName.LastIndexOf(".") + 1).ToLower();
            //文件真实类型
            string strFileType = postedFile.ContentType;
            //文件大小
            int intFileSize = postedFile.ContentLength;
           
            //文件上传的文件夹目录（绝对路径）
            string strFolderMapPath;
            if (!upLoadForder.Equals(""))
            {
                strFolderMapPath = Util.GetMapPath(strRootFolder + upLoadForder);
            }
            else
            {
                strFolderMapPath = Util.GetMapPath(strRootFolder);
            }
            //取当前时间的字符串                    
            string strNowTimeString = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
            //文件的新名称
            string strNewFileName = strNowTimeString + intFileSize.ToString() + "." + strFileExtendName;
            //最终保存文件的路径
            string strFileMapPath = strFolderMapPath + "/" + strNewFileName;
            //返回的文件虚拟目录(相对路径)
            string strFileUploadPath = upLoadForder + "/"  + strNewFileName;
            try
            {
                //加上类型判断(判断是否为系统允许的上传类型,判断文件大小) 
                Directory.CreateDirectory(strFolderMapPath);
                //保存文件
                postedFile.SaveAs(strFileMapPath);
            }
            catch
            {
                oaiInfo.ErrorDescription = "上传文件过程出现故障";
            }
            //HttpContext.Current.Response.Write(strFileUploadPath);
            //附件属性
            oaiInfo.FileName = (strRootFolder + strFileUploadPath).Replace("//","/");
            oaiInfo.FileSize = intFileSize;
            oaiInfo.Attachment = strFileName.Substring(strFileName.LastIndexOf('\\') + 1);
            oaiInfo.FileType = strFileExtendName;
            return oaiInfo;
        }
    }
}